package com.rxyb.toolize.date;

import com.rxyb.toolize.date.enums.FormatDate;
import com.rxyb.toolize.date.enums.Month;
import com.rxyb.toolize.date.enums.Week;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Calendar;

/**
 * @author YJH
 * @version 1.0
 * @description 获取当前之间或者指定时间
 * @date 2020/6/20 17:00
 */
public final class DateTime implements Serializable {


    private static final long serialVersionUID = 2915616761140570703L;


    /**
     * 获取当前年份
     *
     * @return
     */
    public static Integer getYear() {
        return LocalDate.now().getYear();
    }

    public static Integer getYear(Long date) {
        if (date > 100000000L) {
            return LocalDateTime.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getYear();
        }
        return LocalDate.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getYear();
    }


    /**
     * 获取当前月份
     *
     * @return
     */
    public static Month getMonth() {
        Month of = Month.of(LocalDate.now().getMonthValue());
        return of;
    }

    /**
     * 获取日期月份
     *
     * @param date 日期
     * @return
     */
    public static Month getMonth(Long date) {
        if (date > 100000000L) {
            return Month.of(LocalDateTime.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getMonthValue());
        }
        return Month.of(LocalDate.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getMonthValue());
    }

    /**
     * 获取日期月份
     *
     * @param date 日期
     * @return
     */
    public static Month getMonth(String date) {
        if (date.length() > 11) {
            return Month.of(LocalDateTime.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue())).getMonthValue());
        }
        return Month.of(LocalDate.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue())).getMonthValue());
    }

    /**
     * 获取当月的日期
     *
     * @return
     */
    public static Integer getDay() {
        return LocalDate.now().getDayOfMonth();
    }

    /**
     * 获取日期的第几天
     *
     * @param date 日期
     * @return
     */
    public static Integer getDay(Long date) {
        if (date > 100000000L) {
            return LocalDateTime.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getDayOfMonth();
        }
        return LocalDate.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getDayOfMonth();
    }

    /**
     * 获取日期的第几天
     *
     * @param date 日期
     * @return
     */
    public static Integer getDay(String date) {
        if (date.length() > 11) {
            return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue())).getDayOfMonth();
        }
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue())).getDayOfMonth();
    }

    /**
     * 获取当前年份地的第多少天
     *
     * @return
     */
    public static Integer getYearDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取当前星期几
     *
     * @return
     */
    public static Week getWeek() {
        return Week.of(LocalDate.now().getDayOfWeek().getValue());
    }

    /**
     * 获取日期星期几
     *
     * @param date 日期
     * @return
     */
    public static Week getDateWeek(Long date) {
        if (date > 100000000L) {
            return Week.of(LocalDateTime.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getDayOfWeek().getValue());
        }
        return Week.of(LocalDate.parse(String.valueOf(date), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue())).getDayOfWeek().getValue());
    }

    /**
     * 获取日期星期几
     *
     * @param date 日期
     * @return
     */
    public static Week getDateWeek(String date) {
        if (date.length() > 11) {
            return Week.of(LocalDateTime.parse(date, DateTimeFormatter.ofPattern(FormatDate.FORMAT_TIME_LONG.getValue())).getDayOfWeek().getValue());
        }
        return Week.of(LocalDate.parse(date, DateTimeFormatter.ofPattern(FormatDate.FORMAT_DATE_LONG.getValue())).getDayOfWeek().getValue());
    }

    /**
     * 获取当前年份地的第多少周
     *
     * @return
     */
    public static Integer getYearWeek() {
        return Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);
    }


    /**
     * 获取当前日期
     *
     * @return
     */
    public static Long getDate() {
        return DateUtils.parseDayToLong(LocalDate.now());
    }

    /**
     * 自定格式时间
     *
     * @param formatDate
     * @return
     */
    public static String getDate(FormatDate formatDate) {
        return LocalDate.now().format(DateTimeFormatter.ofPattern(formatDate.getValue()));
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static Long getDateTime() {
        return DateUtils.parseTimeToLong(LocalDateTime.now());
    }

    /**
     * 获取自定义格式时间
     *
     * @param formatDate 时间格式查阅时间格式文档
     * @return
     */
    public static String getDateTime(FormatDate formatDate) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatDate.getValue()));
    }

    /**
     * 自定义格式时间
     *
     * @param formatDate 时间格式查阅时间格式文档
     * @return
     */
    public static String getDateFormat(String date, FormatDate formatDate) {
        if (date.trim().length() > 11) {
            return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue()))
                    .format(DateTimeFormatter.ofPattern(formatDate.getValue()));
        }
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue()))
                .format(DateTimeFormatter.ofPattern(formatDate.getValue()));
    }

    /**
     * 自定义格式时间
     *
     * @param time 时间格式查阅时间格式文档
     * @return
     */
    public static String getTimeFormat(long time) {
        LocalTime parse = LocalTime.parse(String.valueOf(time), DateTimeFormatter.ofPattern(FormatDate.FORMAT_TIME_NORM.getValue()));
        return parse.format(DateTimeFormatter.ofPattern(FormatDate.FORMAT_TIME_HOUR.getValue()));
    }

    /**
     * 自定义格式时间
     *
     * @param time 时间格式查阅时间格式文档
     * @return
     */
    public static Long getTimeFormat(String time) {
        LocalTime parse = LocalTime.parse(String.valueOf(time), DateTimeFormatter.ofPattern(FormatDate.FORMAT_TIME_NORM.getValue()));
        return Long.valueOf(parse.format(DateTimeFormatter.ofPattern(FormatDate.FORMAT_TIME_HOUR.getValue())));
    }


    /**
     * 自定义格式时间
     *
     * @param formatDate 时间格式查阅时间格式文档
     * @return
     */
    public static String getDateFormat(Temporal temporal, FormatDate formatDate) {
        if (temporal instanceof LocalDateTime)
            return ((LocalDateTime) temporal).format(DateTimeFormatter.ofPattern(formatDate.getValue()));

        if (temporal instanceof LocalDate)
            return ((LocalDate) temporal).format(DateTimeFormatter.ofPattern(formatDate.getValue()));

        return ((LocalTime) temporal).format(DateTimeFormatter.ofPattern(formatDate.getValue()));
    }

    /**
     * 自定义格式时间
     *
     * @param date 时间格式查阅时间格式文档
     * @return
     */
    public static Long getDateFormatLong(String date) {
        if (date.trim().length() > 11) {
            return DateUtils.parseTimeToLong(LocalDateTime.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue())));
        }
        return DateUtils.parseDayToLong(LocalDate.parse(date, DateTimeFormatter.ofPattern(FormatDate.getValue(date).getValue())));
    }

    /**
     * 自定义格式时间
     *
     * @param formatDate 时间格式查阅时间格式文档
     * @return
     */
    public static String getDateFormat(Long date, FormatDate formatDate) {
        if (date > 10000000L) {
            return LocalDateTime.parse(date.toString(), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue()))
                    .format(DateTimeFormatter.ofPattern(formatDate.getValue()));
        }
        return LocalDate.parse(date.toString(), DateTimeFormatter.ofPattern(FormatDate.getValue(date.toString()).getValue()))
                .format(DateTimeFormatter.ofPattern(formatDate.getValue()));

    }


    static class DateUtils {

        public static Long parseTimeToLong(LocalDateTime time) {
            return Long.parseLong(time.format(DateTimeFormatter.ofPattern(FormatDate.FORMAT_TIME_LONG.getValue())));
        }

        public static Long parseDayToLong(LocalDate localDate) {
            return Long.parseLong(localDate.format(DateTimeFormatter.ofPattern(FormatDate.FORMAT_DATE_LONG.getValue())));
        }
    }


}
